package com.telchina.rcbg.controller;

import com.telchina.common.config.TelchinaConfig;
import com.telchina.common.core.controller.BaseController;
import com.telchina.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 编辑器上传文件
 *
 * @Auther: zlm
 * @Date: 2023/3/30
 * @Description: com.telchina.rcbg.controller
 * @Version: 1.0
 */
@RestController
@RequestMapping("wangEditor")
public class WangEditorUploadController extends BaseController {

    @Value("${app.server.host.url}")
    private String serverPath;

    /**
     * 上传
     *
     * @param file
     * @return
     */
    @PostMapping("upload")
    public Map<String, Object> uploadImg(MultipartFile file)  {
        Map<String, Object> result = new HashMap<>();
        try {
            String path = FileUploadUtils.upload(TelchinaConfig.getUploadPath(), file);
            result.put("errno", 0);
            result.put("data", new HashMap<String, Object>() {{
                put("url", serverPath + path);
            }});
        } catch (IOException e) {
            e.printStackTrace();
            result.put("errno", 1);
            result.put("message", "上传失败");
        }
        return result;
    }
}
